<?php
/**
* A MySQL Library
* This is a basic library for database connection.
* You can connect to the MySQL database by this library and it's very easy to use.
* I will fix bugs and make it more powerful in the future.
* If you find bugs, please contact me or post a new issue on {@link http://code.google.com/p/php-mysql-like-ror/issues/list}
* 
* @author CFC <zusocfc@gmail.com>
* @version 1.0.0
* @package MySQL Object
* @copyright 2006-2008 Hsu Shih-Chung
* 
*/ 

/**
 * MySQL class
 * 
 * Connect to the MySQL by this Object.
 * 
 **/
class MySQL{
/**
* @var string $HOST
* @access private
* @var string $UNAME
* @access private
* @var string $UPWD
* @access private
* @var string $DB
* @access private
* @var string $ENCODE
* @access private
* @var resource $link
* @access private
*/
  private $HOST, $UNAME, $UPWD, $DB, $ENCODE;
  private $link;

/**
* The constructor of MySQL class.
* 
* @param string $host A target hostname or DNS or IP, like mysql_connect("this_param");
* @param string $username Database account username
* @param string $userpwd Database account password
* @param string $database The database you connect
* @param string $encode The encode of connection
* @access public
*/
  function MySQL($host="localhost", $username="root", $userpwd="", $database="db", $encode="utf8"){
    $this->HOST=$host;
    $this->UNAME=$username;
    $this->UPWD=$userpwd;
    $this->DB=$database;
    $this->ENCODE=$encode;
  }
  
/**
* Connect to the database.
* 
* @param string $encode You can change the encoding when connect to the database.
* @return resource
* @access public
*/
  function Conn($encode = "utf8"){
	// 2007.08.10 修正：新增連線時編碼預設，預設值為UTF-8
	  if(isset($this->ENCODE)) $encode = $this->ENCODE;
    $this->link = mysql_connect($this->HOST, $this->UNAME, $this->UPWD);
    $this->Exec("SET CHARACTER SET " . $encode);
    $this->Exec("SET NAMES " . $encode);
    $tmp = mysql_select_db($this->DB, $this->link);
    return $tmp;
  }

/**
* Execute the SQL syntax.
*
* @param string $sql 
* @return resource
* @access public
*/
  function Exec($sql){
	//2006.07.12 修正：原function Query($sql) 改為 function Exec($sql)
  //2006.06.07 修正：原mysql_qurey($str, $this->link); 改為 return mysql_query($str, $this->link);
    return mysql_query($sql,$this->link);
  }

/**
* Close the database connection.
*
* @access public
*/
  function Close(){
    mysql_close($this->link);
  }

  function getCount($sql){
	//2006.06.08後新增
    return mysql_num_rows($this->Exec($sql));
  }

  function getLink(){
	//2007.08.10新增
    if($this->link) return $this->link; else return null;
  }
  
  function getInsertID(){
	// 2008.02.11新增
  // Return the ID which you just inserted.
    return mysql_insert_id($this->link);
  }
  
  function num_rows($table, $field="", $conditions=""){
  // 2008.02.15新增
  // Return the count of the query
	// Default target field is id.
    if(!isset($field))
      $field = "id";
    if(isset($conditions))
      $conditions = " WHERE ".$conditions;
    else
      $conditions = "";
    $sql = "SELECT COUNT(*) FROM ".$table;
    if(strlen($conditions)>1) $sql.=$conditions;
    $tmp = mysql_fetch_assoc($this->Exec($sql));
    return intval($tmp["total"]);
  }
  
  function fetch_assoc($sql){
	// 2008.02.15新增
  // Return the value of mysql_fetch_assoc
    return mysql_fetch_assoc($this->Exec($sql));
  }
  
  function fetch_row($sql){
  // 2008.02.15新增
  // Return the value of mysql_fetch_row
    return mysql_fetch_row($this->Exec($sql));
  }
  
  function fetch_array($sql){
  // 2008.02.15新增
  // Return the value of mysql_fetch_array
    return mysql_fetch_array($this->Exec($sql));
  }

	function free($result){
		// 2008.02.25新增
		// Release the result of the mysql query.
		return mysql_free_result($result);
	}
  
  function OptimizeTable($table){
	// 2008.02.11新增
  // A function that user can optimize their database tables, per table a time.
  // Please check your permission before you use it.
    $rs = mysql_fetch_assoc($this->Exec("OPTIMIZE TABLE `".$table."`"));
    return array("table" => $table, "action" => $rs["Op"], "message_type" => $rs["Msg_type"], "message" => $rs["Msg_text"]);
  }
} // END class 
?>